<?php

namespace app\mobile\controller\home\index;

use app\BaseController;
use app\model\article\ArticleModel;
use app\model\GameModel;
use app\model\music\MusicModel;
use app\model\pic\PicModel;
use app\model\RatingScaleModel;
use app\model\tree\TreeFruitModel;
use app\model\video\VideoModel;
use think\App;

class Index extends BaseController
{
    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    /**
     * 热门测评量表
     * @return bool|string
     */
    public function heatRatingScale(): bool|string
    {
        try {
            $RatingScaleModel = new RatingScaleModel();
            $data = $RatingScaleModel->field('id,name,cover,heat')->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['status','=',1],
                ['is_self_evaluation','=',1]
            ])->order('heat','desc')->limit(0,3)->select()->toArray();
            return success($data);
        } catch (\Exception $e){
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 推荐音乐
     * @return bool|string
     */
    public function heatMusic(): bool|string
    {
        try {
            $param = $this->request->param();
            $this->validate($param,[
                'type_id|类型' => 'require'
            ]);
            $lock = 'heat_music'.$this->userData['id'];
            $data = cache($lock);
            if ($data) return success($data);
            $where[] = ['type_id', '=', $param['type_id']];
            $where[] = ['unit_id','=',$this->userData['unit_id']];
            $MusicModel = new MusicModel();
            $data = $MusicModel->where($where)->limit(0,5)->orderRand()->select()->toArray();
            cache($lock,$data,strtotime(date('Y-m-d 23:59:59')) - time());
            return success($data);
        } catch (\Exception $e){
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }

    /**
     * 心灵放松
     * @return bool|string
     */
    public function relax(): bool|string
    {
        try {
            $MusicModel = new MusicModel();
            $VideoModel = new VideoModel();
            $ArticleModel = new ArticleModel();
            $PicModel = new PicModel();
            $TreeFruitModel = new TreeFruitModel();
            $GameModel = new GameModel();
            $data['music'] = $MusicModel->where('unit_id','=',$this->userData['unit_id'])->find();
            $data['video'] = $VideoModel->where('unit_id','=',$this->userData['unit_id'])->find();
            $data['article'] = $ArticleModel->where('unit_id','=',$this->userData['unit_id'])->find();
            $data['pic'] = $PicModel->where('unit_id','=',$this->userData['unit_id'])->find();
            $data['game'] = $GameModel->find();
            $data['tree'] = $TreeFruitModel->where([
                ['unit_id','=',$this->userData['unit_id']],
                ['status','=',1]
            ])->with([
                'user',
                'treeUserInfo',
                'fruitComment' => function ($query) {
                    $query->with(['user', 'treeUserInfo']);
                },
                'fruitImg'
            ])->limit(0,2)->select()->toArray();
            return success($data);
        } catch (\Exception $e){
            return error($e->getMessage(), $e->getCode(),[
                'line' => $e->getLine(),
                'file' => $e->getFile()
            ]);
        }
    }



}